package graphics.seal;

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ExtendFontCache {
	public final static String SEAL_CHARACTER_KEY = "篆体";
	public final static String CLERICAL_SCRIPT_KEY = "隶书";

	private static ExtendFontCache inc;
	Map<String, Font> cahce = new HashMap<String, Font>();

	private String getResourcePath(String key) {
		String path = System.getProperty("user.dir") + "/resource/fonts/";
		System.out.println(path);
		switch (key) {
		case SEAL_CHARACTER_KEY:
			path += "zhuanti.TTF";
			break;
		case CLERICAL_SCRIPT_KEY:
			path += "微软简隶书.TTF";
			break;
		}
		return path;
	}

	/**
	 * 获取资源字体
	 * 
	 * @param key
	 * @return
	 */
	public Font get(String key) {
		if (!cahce.containsKey(key)) {
			try {
				File fontFile = new File(getResourcePath(key));
				Font nf = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(Font.BOLD);
				cahce.put(key, nf);
			} catch (FontFormatException | IOException e) {
				e.printStackTrace();
			}
		}
		return cahce.get(key);
	}

	private ExtendFontCache() {
	}

	public static synchronized ExtendFontCache inc() {
		if (inc == null)
			inc = new ExtendFontCache();
		return inc;
	}

}
